package tarena.netctoss.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import tarena.netctoss.dao.Pagination;
import tarena.netctoss.dao.UserCondition;
import tarena.netctoss.dao.UserDao;
import tarena.netctoss.po.User;

/**
 * 用户帐务管理模块 对数据库访问层 实现类
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-3-5
 */
public class UserDaoImpl implements UserDao {

	/** hibernate模板 */
	private HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.UserDao#delete(tarena.netctoss.po.User)
	 */
	@Override
	public void delete(User user) {
		hibernateTemplate.delete(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.UserDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll(UserCondition userCondition) {
		return hibernateTemplate.executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return null;
			}
		});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.UserDao#findById(java.lang.Integer)
	 */
	@Override
	public User findById(Integer id) {
		return (User) hibernateTemplate.get(User.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.UserDao#save(tarena.netctoss.po.User)
	 */
	@Override
	public void save(User user) {
		hibernateTemplate.save(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.UserDao#update(tarena.netctoss.po.User)
	 */
	@Override
	public void update(User user) {
		hibernateTemplate.update(user);
	}

	/**
	 * 获得一个Criteria
	 * 
	 * @param userCondition
	 * @param session
	 * @return
	 */

	private Criteria getCriteria(UserCondition userCondition, Session session) {
		Criteria c = session.createCriteria(User.class);
		// 用户名称,
		if (null != userCondition.getName()
				&& !"".equals(userCondition.getName())) {
			c.add(Restrictions
					.like("name", "%" + userCondition.getName() + "%"));
		}
		// 状态
		if (userCondition.getStatus() >= 0) {
			c.add(Restrictions.eq("status", userCondition.getStatus()));
		}
		// 业务帐号
		if (null != userCondition.getLoginname()
				&& !"".equals(userCondition.getLoginname())) {
			c.add(Restrictions.like("loginname", "%"
					+ userCondition.getLoginname() + "%"));
		}
		return c;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see tarena.netctoss.dao.UserDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll() {
		return hibernateTemplate.find("from User u order by u.name");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * tarena.netctoss.dao.UserDao#findAll(tarena.netctoss.dao.UserCondition,
	 * tarena.netctoss.dao.Pagination)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll(final UserCondition userCondition,
			final Pagination page) {
		return hibernateTemplate.executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria c = getCriteria(userCondition, session);

				// 从第几条开始取
				c.setFirstResult((page.getPage() - 1) * page.getPageSize());

				// 最大取多少条
				c.setMaxResults(page.getPageSize());
				return c.list();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * tarena.netctoss.dao.UserDao#loadMaxPage(tarena.netctoss.dao.Pagination,
	 * tarena.netctoss.dao.UserCondition)
	 */
	@Override
	public void loadMaxPage(final Pagination page,
			final UserCondition userCondition) {
		hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria c = getCriteria(userCondition, session);
				// 添加多行函数查询
				// select count(*) from Pricing where name like ....
				c.setProjection(Projections.rowCount());
				Object o = c.uniqueResult();
				// hibernate 低版本count(*)返回 Integer
				// hibernate 高版本count(*)返回 Long
				int rowCount = 0;
				if (o instanceof Long) {
					rowCount = ((Long) o).intValue();
				} else {
					rowCount = ((Integer) o).intValue();
				}
				int maxPage = (rowCount + page.getPageSize() - 1)
						/ page.getPageSize();
				page.setMaxPage(maxPage);
				return null;
			}
		});

	}

}
